#include "SampleTriangle.h"
#include "GLUtils.h"
#include "LogUtils.h"

// 三角形

SampleTriangle::SampleTriangle() {

}

SampleTriangle::~SampleTriangle() {
}


void SampleTriangle::LoadImage(NativeImage *pImage) {
    //null implement

}

void SampleTriangle::Init() {
    // 顶点着色器
    char vShaderStr[] =
            "#version 300 es                          \n"
            "layout(location = 0) in vec4 vPosition;  \n"
            "layout(location = 1) in vec4 aColor;  \n"
            "out vec4 vColor;  \n"
            "void main()                              \n"
            "{                                        \n"
            "   gl_Position = vPosition;              \n"
            "   vColor = aColor;              \n"
            "}                                        \n";
    // 片元着色器
    char fShaderStr[] =
            "#version 300 es                              \n"
            "precision mediump float;                     \n"
            "in vec4 vColor;                          \n"
            "out vec4 fragColor;                          \n"
            "void main()                                  \n"
            "{                                            \n"
            "   fragColor = vColor;  \n"
            "}                                            \n";

    m_ProgramObj = GLUtils::CreateProgram(vShaderStr, fShaderStr, m_VertexShader, m_FragmentShader);

}

void SampleTriangle::Draw() {
    LOG_DEBUG("TriangleSample::Draw\n");

    if (m_ProgramObj == 0)
        return;

    // Use the program object 添加program到OpenGL ES环境中
    glUseProgram(m_ProgramObj);

    // Load the vertex data 准备坐标数据
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices);
    // 启用顶点坐标数组
    glEnableVertexAttribArray(0);
    // 准备颜色数据
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, triangleColorVertices);
     // 启用顶点颜色数据
    glEnableVertexAttribArray(1);

    // 绘画三角形
    glDrawArrays(GL_TRIANGLES, 0, 3);

    // 禁用顶点数据
    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
}

void SampleTriangle::Destroy() {
    if (m_ProgramObj) {
        glDeleteProgram(m_ProgramObj);
    }
}

void SampleTriangle::setParentViewSize(int w, int h) {
    m_ParentViewWidth = w;
    m_ParentViewHeight = h;
}

void SampleTriangle::OnSurfaceCreated() {

}
void SampleTriangle::OnSurfaceChanged(int width, int height) {
    setParentViewSize(width, height);
}
